package com.example.yoho.mvp.view.fragment;


import android.content.Intent;


import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerShoesTrandComponent;
import com.example.yoho.di.moudles.ShoesTrandModules;
import com.example.yoho.mvp.contract.ShoesTrandContract;
import com.example.yoho.mvp.model.entity.CouponlistEntity;
import com.example.yoho.mvp.model.entity.ShoesBean;
import com.example.yoho.mvp.presenter.ShoesTranPresenter;
import com.example.yoho.mvp.view.activity.CouponActivity;
import com.example.yoho.mvp.view.activity.UserActivity;
import com.example.yoho.mvp.view.activity.UserdetailsActivity;
import com.example.yoho.mvp.view.adapter.MyUfoAdapter;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment<ShoesTranPresenter> implements ShoesTrandContract.View, View.OnClickListener {


    private com.jwenfeng.library.pulltorefresh.PullToRefreshLayout minePull;
    private android.support.v7.widget.RecyclerView mineRecyc;
    private List<ShoesBean.ValuesBean> list=new ArrayList<>();
    private MyUfoAdapter adapter;
    private Button button_login;
    private Button login;
    private CardView cardView;
    private RelativeLayout relativeLayout;
    private TextView coupon_num,coupon;

    @Override
    public int bondLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void Inject() {
        DaggerShoesTrandComponent.builder().shoesTrandModules(new ShoesTrandModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {
        mPresenter.requestShoesAll();
        EventBus.getDefault().register(this);
        mineRecyc = (RecyclerView) findviewByIds(R.id.mine_recyc);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void subscriber(Map<String,Object> map){
        String aaa = (String) map.get("aaa");
        Log.i("ysk", "subscriber: "+aaa);
        login.setVisibility(View.GONE);
        cardView.setVisibility(View.VISIBLE);
        relativeLayout.setBackgroundResource(R.drawable.mine_banner_boy);
    }

    @Override
    public void initData() {
        adapter=new MyUfoAdapter(R.layout.ufo_item,list);
        mineRecyc.setAdapter(adapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        gridLayoutManager.setSpanSizeLookup(new SpanSizeLookup());
        mineRecyc.setLayoutManager(gridLayoutManager);
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.layout_mine_header, null);
        ImageView user_icon=inflate.findViewById(R.id.usericon_image);
         coupon_num=inflate.findViewById(R.id.coupon_num);
         coupon=inflate.findViewById(R.id.coupon);
        user_icon.setOnClickListener(this);
        button_login=inflate.findViewById(R.id.login);
        login=inflate.findViewById(R.id.login);
         relativeLayout=inflate.findViewById(R.id.top_back);
         cardView=inflate.findViewById(R.id.my_cardviews);
        button_login.setOnClickListener(this);
        adapter.addHeaderView(inflate);


    }

    @Override
    public void showShoesData(ShoesBean shoesBean) {
        List<ShoesBean.ValuesBean> values = shoesBean.getValues();
        list.addAll(values);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showCouponlistData(CouponlistEntity couponlistEntity) {
        List<CouponlistEntity.ValuesBean> values = couponlistEntity.getValues();
        int size = values.size();
        coupon_num.setText(size+"");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login:
                Intent intent = new Intent(getContext(), UserActivity.class);
                startActivity(intent);
                break;
            case R.id.usericon_image:
                Intent edit_intent = new Intent(getContext(), UserdetailsActivity.class);
                startActivity(edit_intent);
                break;
            case R.id.coupon:
                Intent coupon_intent = new Intent(getContext(), CouponActivity.class);
                startActivity(coupon_intent);
                break;
        }
    }

    class SpanSizeLookup extends GridLayoutManager.SpanSizeLookup {

        @Override
        public int getSpanSize(int i) {
            if (i==0){
                return 2;
            }else{
                return 1;
            }
        }
    }
}
